#!/bin/bash

monArr=("January"
		"February"
		"March"
		"April"
		"May"
		"June"
		"July"
		"August"
		"September"
		"October"
		"November"
		"December")

monShortArr=("Jan"
		"Feb"
		"Mar"
		"Apr"
		"May"
		"Jun"
		"Jul"
		"Aug"
		"Sep"
		"Oct"
		"Nov"
		"Dec")

# 两种获取数组长度的方法
echo "数组元素个数为: ${#monArr[*]}"
arrlen=${#monArr[@]}

# 累加下标遍历
echo ""
echo 累加下标遍历
for ((i=0; i<arrlen; ++i))
do
	echo index:$i value:${monArr[$i]}
done

# 直接遍历
echo ""
echo 直接遍历
loop=0
for i in ${monArr[@]}
do
	echo index:$loop value:$i
	loop=$((loop+1))
done

# 循环下标遍历
echo ""
echo 循环下标遍历
for i in "${!monArr[@]}";
do
	echo index:$i value:${monArr[$i]}
done


# 
echo ""
echo 升序输出
echo ${monArr[@]}|tr ' ' '\n'|sort
echo ""
echo 降序输出
echo ${monArr[@]}|tr ' ' '\n'|sort -r

# 直接输出
echo ${monArr[@]}
echo ${monArr[@]:3}
echo ${monArr[@]:3:4}
echo ${monArr[@]:10:4}

# 把数组 直接变成 字符，分割符是 空格
strFromArr=$(echo ${monArr[@]})
echo strFromArr = $strFromArr

# 把数组 直接变成 字符，把分割符从 空格 换成 逗号
strFromArr=$(echo ${monArr[@]}|tr ' ' ',')
echo strFromArr = $strFromArr

# 把字符串（不含空格）按照 分隔符 拆开，构建成一个数组。
loop=0
declare arrFromStr
for str in $(echo $strFromArr|tr ',' '\n')
do
	arrFromStr[$loop]=$str
	loop=$((loop+1))
done

echo arrFromStr : ${arrFromStr[@]}